<!doctype html>
<html>
<head>
	<meta charset="utf-8">
	<title>组件切换时使用keep-alive缓存不活动的组件实例</title>
	<script src="vue.js"></script>
</head>
<body>
<div id="app">
    <button @click="change">切换页面</button>
    <keep-alive>
        <component :is="currentView"></component>  
    </keep-alive>
</div>
<script>
  var vm=new Vue({
      el: '#app',
      data:{
        index:0,
        arr:[
            { template:`<div>打开主页</div>` },
            { template:`<div>打开登录页面</div>` },
            { template:`<div>打开注册页面</div>` }
        ],
      },
      computed:{
        currentView(){
            return this.arr[this.index];
        }
      },
      methods:{
        change(){
          let len = this.arr.length;
          this.index = (++this.index)% len;
        }
      }
  })
</script>
</body>
</html>